#!/usr/bin/env nu

use images/state.nu *
use images/create.nu *
use images/list.nu *
use images/delete.nu *
use images/update.nu *
use images/watch.nu *

export def "main help" [--notitles]: nothing -> nothing {
    exec $"($env.PROVISIONING_NAME)" help build --notitles
}

def main [
    subcmd:   string = "help"
    role:     string = ""
    --check (-c)
    --infra:        string = ""
    --provider:     string = "hetzner"
    --yes (-y)
    --interval:     int    = 60
    --auto-build
    --notify-only
]: nothing -> nothing {
    match $subcmd {
        "create" => {
            if ($role | is-empty) {
                print "Usage: provisioning build image create <role> [--infra <path>] [--check]"
                exit 1
            }
            image-create $role --infra $infra --check=$check
        }
        "list" => {
            image-list --provider $provider
        }
        "delete" => {
            if ($role | is-empty) {
                print "Usage: provisioning build image delete <role> [--provider <p>] [--yes]"
                exit 1
            }
            image-delete $role --provider $provider --yes=$yes
        }
        "update" => {
            if ($role | is-empty) {
                print "Usage: provisioning build image update <role> [--infra <path>] [--provider <p>]"
                exit 1
            }
            image-update $role --provider $provider --infra $infra --check=$check
        }
        "watch" => {
            image-watch --provider $provider --infra $infra --interval $interval --auto-build=$auto_build --notify-only=$notify_only
        }
        "help" | "h" | _ => {
            exec $"($env.PROVISIONING_NAME)" help build --notitles
        }
    }
}
